//
//  UIAlertViewController+Extension.swift
//  zhengwutong
//
//  Created by Bernie on 16/9/18.
//  Copyright © 2016年 wsg. All rights reserved.
//

import UIKit
class UIAlertControllerManager:NSObject{
    
    public static let share:UIAlertControllerManager = {
        return UIAlertControllerManager()
    }()
    var alerts:[UIAlertController] = []
    func showNext() {
        alerts.removeFirst()
        showAlert()
    }
    func addAlerts(alert:UIAlertController) -> Void {
        alerts.append(alert)
        if alerts.count==1 {
            showAlert()
        }
    }
    func showAlert() {
        DispatchQueue.main.async {[weak self] in
            if let SSelf = self,let lastAlert:UIAlertController = SSelf.alerts.first{
                
                if #available(iOS 13.0, *) {
                    if let rootViewController = UIApplication.shared.windows.first?.rootViewController{
                        if let pvc = rootViewController.presentedViewController {
                            (SSelf.topPresentedViewController(pvc)).present(lastAlert, animated: false, completion: nil)
                        } else {
                            rootViewController.present(lastAlert, animated: false, completion: nil)
                        }
                    }
                } else {
                    // Fallback on earlier versions
                    if let rootViewController = UIApplication.shared.keyWindow?.rootViewController{
                        if let pvc = rootViewController.presentedViewController {
                            (SSelf.topPresentedViewController(pvc)).present(lastAlert, animated: false, completion: nil)
                        } else {
                            rootViewController.present(lastAlert, animated: false, completion: nil)
                        }
                    }
                }
            }
        }
    }
    func topPresentedViewController(_ contrl:UIViewController) -> UIViewController{
        var temp = contrl.presentedViewController
        while ((temp?.presentedViewController) != nil) {
            temp = temp?.presentedViewController
        }
        return temp ?? UIApplication.shared.windows.first!.rootViewController!
    }
    
}
extension UIAlertController {
    
    
    
    static func alertMessage(_ str:String,title:String = "",btnTitles:[String]=[],action:@escaping ((Int)->Void) = {_ in }){
        let vc = UIAlertController(title: title, message: str, preferredStyle: .alert)
        if btnTitles.count == 0 {
            vc.addAction(UIAlertAction(title: "确定", style: .default, handler: { (ac) in
                action(0)
                UIAlertControllerManager.share.showNext()
            }))
        }else{
            for item in btnTitles {
                vc.addAction(UIAlertAction(title: item, style: .default, handler: { (ac) in
                    action(btnTitles.firstIndex(of: ac.title!)!)
                    UIAlertControllerManager.share.showNext()
                }))
            }
        }
        UIAlertControllerManager.share.addAlerts(alert: vc)
    }
    static func alertTextField(title:String = "",textFieldPlaceholder:[(String,Bool)]=[],btnTitles:[String]=[],action:@escaping ((UIAlertController,Int)->Void) = {_,_ in }){
        let vc = UIAlertController(title: title, message: "", preferredStyle: .alert)
        for item in textFieldPlaceholder {
            vc.addTextField(configurationHandler: { (tf) in
                tf.placeholder = item.0
                tf.isSecureTextEntry = item.1
            })
        }
        if btnTitles.count == 0 {
            vc.addAction(UIAlertAction(title: "确定", style: .default, handler: { (ac) in
                action(vc,0)
                UIAlertControllerManager.share.showNext()
            }))
        }else{
            for item in btnTitles {
                vc.addAction(UIAlertAction(title: item, style: .default, handler: { (ac) in
                    action(vc,btnTitles.firstIndex(of: ac.title!)!)
                    UIAlertControllerManager.share.showNext()
                }))
            }
        }
        UIAlertControllerManager.share.addAlerts(alert: vc)
    }
    static func actionSheetMessage(_ str:String,btnTitles:[String]=[],action:@escaping ((Int)->Void) = {_ in }){
        let vc = UIAlertController(title: str, message: "", preferredStyle: .actionSheet)
        if(btnTitles.count==0){
            vc.addAction(UIAlertAction(title: "确定", style: .default, handler: { (ac) in
                action(0)
                UIAlertControllerManager.share.showNext()
            }))
        }else{
            for item in btnTitles {
                vc.addAction(UIAlertAction(title: item, style: .default, handler: { (ac) in
                    action(btnTitles.firstIndex(of: ac.title!)!)
                    UIAlertControllerManager.share.showNext()
                }))
            }
        }
        UIAlertControllerManager.share.addAlerts(alert: vc)
    }
    
}
